<template>
  <div :id="pid" class="record-player" :class="{'visible': record}" @click.stop="doPause()" oncontextmenu="return false" ondragstart="return false">
    <EasyPlayer ref="player" v-if="url" class="player" :videoUrl="url" :playerName="channelName" :muted="muted" :easyStretch="stretch" :has-audio="$store.getters.flvHasAudio || !url.includes('.flv')" :reconnection="true" live @click.native.stop />
    <div class="time-axis" @click.stop @dblclick.stop @mousemove.stop>
      <i class="iconfont record-pause" :class="pause ? 'icon-icon_shipinbofang' : 'icon-icon_shipinzanting'" @click="doPause()" />
      <div class="axis-body">
        <div ref="axis" class="axis" @mousemove="axisMousemove" @mouseout="axisMouseout" @click="axisDblclick">
          <b ref="axisZhu" v-for="(item, index) in axis" :key="index" :style="{'left': item.x, 'width': item.per}" />
        </div>
        <div v-if="time" ref="time" class="time" :style="{'left': time.x}">{{ time.text }}</div>
        <div class="progress" :style="{'left': progress.x}" />
      </div>
    </div>
  </div>
</template>
<script>
import { getVideoPlaybackUrl } from '@/api/video/video';
export default {
  data() {
    return {
      pid: this.UUID(), //页面唯一id
      url: '', //录像url
      pause: false, //暂停
      muted: true, //静音
      stretch: true, //是否强制铺满
      axis: [], //时间轴
      time: false, //时间轴时间浮显
      progress: false
    };
  },
  props: ['deviceCode', 'channelCode', 'channelName', 'record'],
  watch: {
    record: {
      handler(record) {
        console.log('================播放================', this.record);
        this.url = '';
        this.axis = [];
        this.time = false;
        this.progress = false;
        if(record) {
          this.createAxis();
          this.doPlay();
        }
      },
      immediate: true
    }
  },
  methods: {
    /**
     * 抓图
     */
    screenshot() {
      try{
        this.$refs.player.snapshot();
      }catch(e){
        if(!flag) {
          $message.e('抓图失败');
        }
      }
    },
    /**
     * 切换静音
     */
    changeMuted(muted = !this.muted) {
      this.muted = muted;
    },
    /**
     * 切换是否铺满播放
     */
    changeStretch(stretch = !this.stretch) {
      this.stretch = stretch;
    },
    /**
     * 播放
     */
    doPlay(startTime = this.record.startF) {
      this.pause = false;
      const params = {
        deviceCode: this.deviceCode, //设备编码
        channelCode: this.channelCode, //通道编码
        startTime, //开始时间
        endTime: this.record.endF, //结束时间
        storageType: this.record.storageType, //存储类型 0-前端录像, 1-后端录像
        protocolType: this.$store.getters.protocolTypeNew, //协议类型
        netType: this.$store.getters.netType,
        frontStreamUuid: this.streamUuid // 流uuid
      };
      getVideoPlaybackUrl(params).then(resp => {
        if(resp.code === 200) {
          this.url = resp.data.streamUrl;
          this.streamUuid = resp.data.streamUuid;
        }else {
          $message.a(resp);
        }
      });
    },
    /**
     * 暂停
     */
    doPause() {
      if(!this.url) {
        return false;
      }
      this.pause = !this.pause;
      if (!this.pause) {
        $('#'+ this.pid).find('.vjs-play-control.vjs-paused, .video-tools .icon-bofang').click();
      } else {
        $('#'+ this.pid).find('.vjs-play-control.vjs-playing, .video-tools .icon-zanting').click();
      }
    },
    /**
     * 创建时间轴
     */
    createAxis() {
      const { start, startF, end, endF, records } = JSON.parse(JSON.stringify(this.record));
      records.forEach(item => {
        if(item.startTime < startF) {
          item.startTime = startF;
        }
        if(item.endTime > endF) {
          item.endTime = endF;
        }
        const rs = new Date(item.startTime).getTime();
        const re = new Date(item.endTime).getTime();
        let per = (re - rs) / (end - start) * 100;
        const x = (rs - start) / (end - start) * 100;
        if(x + per > 100) {
          per = 100 - x;
        }
        this.axis.push({
          per: per + '%',
          x: x + '%'
        });
      });
      this.$nextTick(() => {
        this.progress = {
          x: this.axis[0].x
        };
      });
    },
    /**
     * 时间轴双击
     */
    axisDblclick(e) {
      const { start, end } = this.record;
      const { t, f } = this.getAxisTime(e);
      if(this.record.records.some(item => item.startTime <= f && item.endTime >= f)) {
        this.progress = {
          x: (t - start) / (end - start) * 100 + '%'
        };
        this.doPlay(f);
      }
    },
    /**
     * 时间轴时间显示
     */
    axisMousemove(e) {
      let x = e.offsetX;
      this.time = {
        x: x + 'px',
        text: this.getAxisTime(e).f
      };
      this.$nextTick(() => {
        const w = this.$refs.axis.offsetWidth;
        const tw = this.$refs.time.offsetWidth / 2;
        if(x + tw > w) {
          x = w - tw;
        }
        if(x - tw < 0) {
          x = tw;
        }
        this.time.x = x + 'px';
      });
    },
    /**
     * 时间轴时间隐藏
     */
    axisMouseout() {
      this.time = false;
    },
    /**
     * 获取时间轴时间
     */
    getAxisTime(e) {
      const w = this.$refs.axis.offsetWidth;
      const x = e.offsetX;
      const { start, end } = this.record;
      const t = start + (end - start) * x / w;
      return {
        t,
        f: this.$moment(t).format('YYYY-MM-DD HH:mm:ss')
      };
    },
  }
}
</script>
<style lang="scss" scoped>
.record-player{
  width: 100%;
  height: 100%;
  position: absolute;
  left: 0;
  top: 0;
  z-index: 9999;
  display: none;
  &.visible{
    display: block;
  }
  .player{
    width: 100%;
    height: 100%;
    pointer-events: none;
    /deep/ .vjs-control-bar,
    /deep/ .vsc-controller,
    /deep/ .vjs-big-play-button,
    /deep/ .easy-player-right-menu,
    /deep/ .vjs-control.vjs-button.vjs-stretch-control{
      display: none !important;
    }
  }
  .time-axis{
    width: 100%;
    height: 40px;
    background: rgba(0,0,0,0.7);
    position: absolute;
    left: 0;
    bottom: 0;
    z-index: 2;
    display: flex;
    align-items: center;
    cursor: default;
    .record-pause{
      width: 50px;
      text-align: center;
      line-height: 1;
      font-size: 28px;
      color: #fff;
      cursor: pointer;
    }
    .axis-body{
      flex: 1;
      height: 100%;
      display: flex;
      align-items: center;
      position: relative;
    }
    .axis{
      width: 100%;
      height: 16px;
      background: rgba(255,255,255,0.2);
      cursor: pointer;
      position: relative;
      b{
        position: absolute;
        height: 100%;
        width: 0;
        left: 0;
        top: 0;
        background: #fff;
        pointer-events: none;
      }
    }
    .time{
      position: absolute;
      bottom: calc(100% + 2px);
      left: 0;
      transform: translateX(-50%);
      background: rgba(0,0,0,0.4);
      color: #fff;
      font-size: 12px;
      padding: 4px 8px;
      white-space: nowrap;
      pointer-events: none;
      z-index: 8;
    }
    .progress{
      width: 2px;
      height: 100%;
      position: absolute;
      left: 0;
      top: 0;
      background: #fff;;
      pointer-events: none;
      transform: translateX(-50%);
      &:before{
        content: "";
        width: 8px;
        height: 5px;
        background: #fff;;
        position: absolute;
        left: 50%;
        top: 0;
        transform: translateX(-50%);
        z-index: 3;
      }
      &:after{
        content: "";
        width: 8px;
        height: 0;
        border: 4px solid transparent;
        border-top: 3px solid #E8F3FE;
        position: absolute;
        left: 50%;
        top: 5px;
        transform: translateX(-50%);
        z-index: 2;
      }
    }
  }
}
</style>
